package com.ruoyi.common.core.page;


import com.ruoyi.common.utils.StringPool;
import com.ruoyi.common.utils.StringUtils;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 分页数据
 *
 * @author ruoyi
 */
public class PageDomain implements Serializable {
    private static final long serialVersionUID = 6847766244605307189L;
    /**
     * 当前记录起始索引
     */
    @Setter
    @Getter
    private Integer pageNum;

    /**
     * 每页显示记录数
     */
    @Setter
    @Getter
    private Integer pageSize;

    /**
     * 排序列
     */
    @Setter
    @Getter
    private String orderByColumn;

    /**
     * 排序的方向desc或者asc
     */
    @Getter
    private String isAsc = "asc";

    /**
     * 分页参数合理化
     */
    @Setter
    private Boolean reasonable = true;

    public String getOrderBy() {
        if (StringUtils.isEmpty(orderByColumn)) {
            return StringPool.EMPTY;
        }
        return StringUtils.toUnderScoreCase(orderByColumn) + StringPool.SPACE + isAsc;
    }

    /**
     * 是否正序排序
     *
     * @return 是 正序 否 倒序
     */
    public boolean whetherAsc() {
        return StringUtils.equals(isAsc, "asc");
    }

    public String getOrderByField() {
        if (StringUtils.isEmpty(orderByColumn)) {
            return StringPool.EMPTY;
        }
        return StringUtils.toUnderScoreCase(orderByColumn);
    }

    public void setIsAsc(String isAsc) {
        if (StringUtils.isNotEmpty(isAsc)) {
            // 兼容前端排序类型
            if ("ascending".equals(isAsc)) {
                isAsc = "asc";
            } else if ("descending".equals(isAsc)) {
                isAsc = "desc";
            }
            this.isAsc = isAsc;
        }
    }

    public Boolean getReasonable() {
        if (StringUtils.isNull(reasonable)) {
            return Boolean.TRUE;
        }
        return reasonable;
    }

}
